package edu.nf.supermarket.web.controller.admin;

import edu.nf.supermarket.base.BaseController;
import edu.nf.supermarket.result.ResultVO;
import edu.nf.supermarket.service.OrderService;
import edu.nf.supermarket.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yjj
 * @date 2025-06-16
 * 数据统计控制器
 */
@RestController
@RequestMapping("/salesStatistics")
public class SalesStatisticsController extends BaseController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;

    /**
     * 计算年销售额
     *
     * @return
     */
    @GetMapping("/calculateYearsSales")
    public ResultVO<Map<String, BigDecimal>> calculateSales() {
        Map<String, BigDecimal> sales = new HashMap<>();
        //获取今年的营业额
        LocalDateTime startTime = LocalDate.now().with(TemporalAdjusters.firstDayOfYear()).atStartOfDay();
        LocalDateTime endTime = LocalDate.now().with(TemporalAdjusters.lastDayOfYear()).atTime(LocalTime.MAX);
        BigDecimal thisYearSales = orderService.calculateMonthSales(startTime, endTime);
        //获取去年的营业额
        BigDecimal preSales = orderService.calculateMonthSales(startTime.minusYears(1), endTime.minusYears(1));
        sales.put("thisYearSales", thisYearSales);
        sales.put("preSales", preSales);
        return success(sales);
    }

    /**
     * 计算当月销售额
     *
     * @return
     */
    @GetMapping("/calculateMonthSales")
    public ResultVO<Map<String, BigDecimal>> calculateMonthSales() {
        Map<String, BigDecimal> monthSales = new HashMap<>();
        //获取当月第一天
        LocalDateTime firstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
        //获取当月最后一天
        LocalDateTime lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);

        BigDecimal thisMonthSales = orderService.calculateMonthSales(firstDay, lastDay);

        monthSales.put("thisMonthSales", thisMonthSales);
        //获取上月第一天
        LocalDateTime preFirstDay = LocalDate.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
        //获取上月最后一天
        LocalDateTime preLastDay = LocalDate.now().minusMonths(1).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
        BigDecimal preMonthSales = orderService.calculateMonthSales(preFirstDay, preLastDay);

        monthSales.put("preMonthSales", preMonthSales);
        return success(monthSales);
    }

    /**
     * 计算当日销售额
     */
    @GetMapping("/calculateTodaySales")
    public ResultVO<Map<String, BigDecimal>> calculateTodaySales() {
        Map<String, BigDecimal> daySales = new HashMap<>();
        //获取今天从0点开始，23点结束
        BigDecimal todaySales = orderService.calculateMonthSales(
                LocalDate.now().atTime(LocalTime.MIN), LocalDate.now().atTime(LocalTime.MAX));
        daySales.put("todaySales", todaySales);
        //获取昨天从0点开始，23点结束
        BigDecimal yesterdaySales = orderService.calculateMonthSales(
                LocalDate.now().minusDays(1).atTime(LocalTime.MIN), LocalDate.now().minusDays(1).atTime(LocalTime.MAX));
        daySales.put("yesterdaySales", yesterdaySales);
        return success(daySales);
    }

    /**
     * 获取用户总数
     */
    @GetMapping("/getUserCount")
    public ResultVO<Integer> getUserCount() {
        return success(userService.getUserCount());
    }
    /**
     * 获取从这个月起往前六个月的每月销售额
     */
    @GetMapping("/getMonthSales")
    public ResultVO<Map<String, BigDecimal>> getMonthSales() {
        Map<String, BigDecimal> monthSales = new HashMap<>();
        for (int i = 5; i >= 0; i--) {
            LocalDateTime startTime = LocalDate.now().minusMonths(i).with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay();
            LocalDateTime endTime = LocalDate.now().minusMonths(i).with(TemporalAdjusters.lastDayOfMonth()).atTime(LocalTime.MAX);
            BigDecimal sales = orderService.calculateMonthSales(startTime, endTime);
            monthSales.put(startTime.getMonth().toString(), sales);
        }
        return success(monthSales);
    }
    /**
     * 用户构成
     */
    @GetMapping("/countUserTypes")
    public ResultVO<Map<String, Long>> getUserStats() {
        Map<String, Long> stats = userService.countUserTypes();
        return success(stats);
    }
}
